package git.bds.nyc.applet.api.controller.order;

import git.bds.nyc.applet.api.convert.OrderConvert;
import git.bds.nyc.applet.api.model.vo.order.OrderVO;
import git.bds.nyc.applet.api.service.order.OrderService;
import git.bds.nyc.communal.model.domain.ContractOrder;
import git.bds.nyc.page.PageParam;
import git.bds.nyc.page.PageResult;
import git.bds.nyc.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 成大事
 * @since 2023/2/14 21:28
 */
@Api(tags = "订单模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/applet/order")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OrderController {

    private final OrderService orderService;

    /**
     * 查看自己下单别人产品的订单list 分页
     *
     * @return {@link R}<{@link Boolean}>
     */
    @PostMapping("/getMyOrderPage")
    @ApiOperation("查看自己下单别人产品的订单分页")
    public R<PageResult<OrderVO>> getMyOrderPage(
            @RequestBody @Validated PageParam pageParam
    ){
        PageResult<ContractOrder> page = orderService.getMyOrderPage(pageParam);
        List<OrderVO> list = OrderConvert.INSTANCE.toOrderVO(page.getList());
        return R.ok(new PageResult<>(list,page.getTotal()));
    }


    /**
     * 查看自己下单别人产品的订单 分页
     *
     * @return {@link R}<{@link Boolean}>
     */
    @PostMapping("/getMyOrderPageByType/{type}")
    @ApiOperation("查看自己下单别人产品的订单分页")
    public R<PageResult<OrderVO>> getMyOrderPageByType(
            @RequestBody @Validated PageParam pageParam,
            @PathVariable("type") @Min(value = 1,message = "类型错误！！！") @Max(value = 4,message = "类型错误！！！") int type
    ){
        PageResult<ContractOrder> page = orderService.getMyOrderPageByType(pageParam,type);
        List<OrderVO> list = OrderConvert.INSTANCE.toOrderVO(page.getList());
        return R.ok(new PageResult<>(list,page.getTotal()));
    }


    /**
     * 获取指定时间段订单 分页
     *
     * @param pageParam 页面参数
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return {@link R}<{@link PageResult}<{@link OrderVO}>>
     */
    @PostMapping("/getOrderOfTheSpecifiedTimePeriodPage/{startTime}/{endTime}")
    @ApiOperation(value = "获取指定时间段的订单记录 分页",notes = "不用指定订单类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataTypeClass = LocalDateTime.class, paramType = "path", example = "2023-2-12", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataTypeClass = LocalDateTime.class, paramType = "path", example = "2023-2-14", required = true)
    })
    public R<PageResult<OrderVO>> getOrderOfTheSpecifiedTimePeriodPage(
            @Validated PageParam pageParam,
            @PathVariable("startTime") LocalDateTime startTime,
            @PathVariable("endTime") LocalDateTime endTime
    ){
        PageResult<ContractOrder> page = orderService.getOrderOfTheSpecifiedTimePeriodPage(pageParam,startTime,endTime);
        List<OrderVO> list = OrderConvert.INSTANCE.toOrderVO(page.getList());
        return R.ok(new PageResult<>(list,page.getTotal()));
    }


    /**
     * 根据订单id删除订单
     *
     * @return {@link R}<{@link Boolean}>
     */
    @PostMapping("/delOrderById/{type}/{id}")
    @ApiOperation(value = "根据订单id删除订单",notes = "类型只能有这两种(3:拒绝签字、4:交易成功)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型(3:拒绝签字、4:交易成功", dataTypeClass = Integer.class, paramType = "path", example = "1", required = true),
            @ApiImplicitParam(name = "id", value = "订单id", dataTypeClass = Long.class, paramType = "path", example = "1", required = true)

    })
    public R<Boolean> delOrderById(
            @PathVariable("type") @Min(value = 3,message = "类型错误！！！") @Max(value = 4,message = "类型错误！！！") int type,
            @PathVariable("id") Long id
    ){
        return R.decide( orderService.delOrderById(type,id) );
    }


    /**
     * 根据订单id取消未签名的订单
     *
     * @param id 订单id
     * @return {@link R}<{@link Boolean}>
     */
    @PostMapping("/cancelUnsignedOrderById/{id}")
    @ApiOperation(value = "根据订单id取消未签名的订单",notes = "只有未签名的才能被取消")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", dataTypeClass = Long.class, paramType = "path", example = "1", required = true)

    })
    public R<Boolean> cancelUnsignedOrderById(
            @PathVariable("id") @NotNull Long id
    ){
        return R.decide( orderService.cancelUnsignedOrderById(id) );
    }
}
